<?php
header("Content-type: text/html; charset=utf-8"); 


	// 显示curl 抓取结果
	// echo $is_bad_word;
	
	function get_is_bad_word($str)
	{
		$url = 'http://cn.bing.com/search?q='.urlencode($str);
		$url="https://s.taobao.com/search?q=".urlencode($str);
		$rs  = curl_post($url);
		return $rs;
	}

	function curl_get($f_url){
		//初始化
		$ch = curl_init();
		//设置选项，包括URL
		curl_setopt($ch, CURLOPT_URL, $f_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//执行并获取HTML文档内容
		$output = curl_exec($ch);
		//释放curl句柄
		curl_close($ch);

		//返回获得的数据
		return $output;
	}

		 function curl_post($url, $data='', $method='GET'){   
    $curl = curl_init(); // 启动一个CURL会话  
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
    // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer  
    if($method=='POST'){  
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求  
        if ($data != ''){  
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包  
        }  
    }  
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回  
    $tmpInfo = curl_exec($curl); // 执行操作  
    curl_close($curl); // 关闭CURL会话  
    return $tmpInfo; // 返回数据   
} 
function preg_A($str){
	$char = "。、！？：；﹑•＂…‘’“”〝〞∕¦‖—　〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔！¡？¿﹖﹌﹏﹋＇´ˊˋ―﹫︳︴¯＿￣﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼（），≥";
 
$pattern = array(
    "/[[:punct:]]/i", //英文标点符号
    '/['.$char.']/u', //中文标点符号
    "/\r/",
    "/\n/",
    "/\t/",
    "/ /",
    '/[ ]{2,}/'
);
$str = preg_replace($pattern, '', $str);
return $str;
}
function trimall($str){
    $qian=array(" ","　","\t","\n","\r");
    return str_replace($qian, '', $str);   
}

function mbStrSplit ($string, $len=1) {
  $start = 0;
  $strlen = mb_strlen($string);
  while ($strlen) {
  	$field=mb_substr($string,$start,$len,"utf8");

  	if ($field!="\n" && $field!="\r\n" && $field!="\t" ) {
  		$array[] = $field;
  		# code...
  	}

    $string = mb_substr($string, $len, $strlen,"utf8");
    $strlen = mb_strlen($string);
  }

  return $array;
}


$pdo = new PDO("mysql:host=localhost;dbname=curl","root","root");
$re=$pdo->query("set names utf8");
$sql2="select content from sys_document_article";
put_in($sql2,$pdo);

function put_in($sql,$pdo){
$field = explode(' ',$sql);
$field = trim($field[1]);
	$re=$pdo->query($sql); 
	while ( $row=$re->fetch(PDO::FETCH_ASSOC)) {
	$arr[]=$row[$field];
	# code...
}
	// $error_sql="select * from illegal";
	// $error_arr=put_in($error_sql,$pdo);
	// var_dump($error_arr);


foreach ($arr as $k => $v) {


$v=preg_A(strip_tags($v));
		if ($v) {
				if ($v!='') {
				$split_Arr = mbStrSplit($v,2) ;
				# code...
				}
		   }


  foreach ($split_Arr as $kk => $vv) {

	$is_bad_word = get_is_bad_word($vv);
	sleep(1);
	$len=strlen($is_bad_word);

	if ($len<120000) {
		$sql="insert into illegal (title) value ('".$vv."')";
		$write_in=$pdo->exec($sql);
			if ($write_in) {
				echo 1;
				# code...
			}

			# code...
			}else {
				echo 0;
			}
		# code...
       }

	# code...
   }
echo 'ok!';

}



